//
//  JKLivingBeautyView.swift
//  JKLivingBeautyCamera
//
//  Created by 王冲 on 2020/8/4.
//  Copyright © 2020 王冲. All rights reserved.
//

import UIKit

typealias BeautySetingBlock = (Int, Float) -> (Void)
/// 是否开启美颜的 闭包
typealias BeautyCameraBlock = (Bool) -> (Void)

class JKLivingBeautyView: UIView {

    /// 对应的block
    var beautySetingBlock: BeautySetingBlock?
    var beautyCameraBlock: BeautyCameraBlock?
    
    /// 是否开启美颜的按钮
    fileprivate var beautyLabel: UILabel!
    /// 美颜开启和关闭的按钮
    fileprivate var beaurySwitch: UISwitch!
    /// 磨皮
    fileprivate var labe1: UILabel!
    var slider1: UISlider!
    /// 曝光
    fileprivate var labe2: UILabel!
    var slider2: UISlider!
    /// 美白
    fileprivate var labe3: UILabel!
    var slider3: UISlider!
    /// 饱和
    fileprivate var labe4: UILabel!
    var slider4: UISlider!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    func initUI() {
        beautyLabel = UILabel(frame: CGRect(x: 10, y: 10, width: 75, height: 30))
        beautyLabel.backgroundColor = .brown
        beautyLabel.textColor = .red
        beautyLabel.textAlignment = .center
        beautyLabel.font = UIFont.systemFont(ofSize: 15)
        beautyLabel.layer.cornerRadius = 8
        beautyLabel.clipsToBounds = true
        beautyLabel.text = "关闭美颜"
        addSubview(beautyLabel)
        
        beaurySwitch = UISwitch(frame: CGRect(x: beautyLabel.frame.maxX + 10, y: beautyLabel.frame.midY-20, width: 50, height: 40))
        beaurySwitch.addTarget(self, action: #selector(change), for: .valueChanged)
        beaurySwitch.center.y = beautyLabel.frame.midY
        beaurySwitch.onTintColor = .green
        beaurySwitch.tintColor = .yellow
        beaurySwitch.thumbTintColor = .orange
        beaurySwitch.isOn = true
        addSubview(beaurySwitch)
        
        labe1 = UILabel(frame: CGRect(x: 10, y: beautyLabel.frame.maxY + 20, width: 40, height: 20))
        labe1.text = "磨皮"
        labe1.textColor = .white
        labe1.textAlignment = .center
        addSubview(labe1)
        
        labe2 = UILabel(frame: CGRect(x: 10, y: labe1.frame.maxY + 20, width: 40, height: 20))
        labe2.text = "曝光"
        labe2.textColor = .white
        labe2.textAlignment = .center
        addSubview(labe2)
        
        labe3 = UILabel(frame: CGRect(x: 10, y: labe2.frame.maxY + 20, width: 40, height: 20))
        labe3.text = "美白"
        labe3.textColor = .white
        labe3.textAlignment = .center
        addSubview(labe3)
        
        labe4 = UILabel(frame: CGRect(x: 10, y: labe3.frame.maxY + 20, width: 40, height: 20))
        labe4.text = "饱和"
        labe4.textColor = .white
        labe4.textAlignment = .center
        addSubview(labe4)
        
        slider1 = UISlider(frame: CGRect(x: labe1.frame.maxX + 20, y: labe1.frame.midY - 10, width: UIScreen.main.bounds.size.width - 20 - labe1.frame.maxX - 20, height: 20))
        slider1.addTarget(self, action: #selector(slider1Cnange), for: .valueChanged)
        slider1.minimumValue = 0
        slider1.maximumValue = 1
        slider1.value = 0.5
        slider1.minimumTrackTintColor = .green
        slider1.maximumTrackTintColor = .brown
        slider1.thumbTintColor = .yellow
        addSubview(slider1)
        
        slider2 = UISlider(frame: CGRect(x: labe2.frame.maxX + 20, y: labe2.frame.midY - 10, width: UIScreen.main.bounds.size.width - 20 - labe2.frame.maxX - 20, height: 20))
        slider2.addTarget(self, action: #selector(slider2Cnange), for: .valueChanged)
        slider2.minimumValue = -10
        slider2.maximumValue = 10
        slider2.value = 0
        slider2.minimumTrackTintColor = .green
        slider2.maximumTrackTintColor = .brown
        slider2.thumbTintColor = .yellow
        addSubview(slider2)
        
        slider3 = UISlider(frame: CGRect(x: labe3.frame.maxX + 20, y: labe3.frame.midY - 10, width: UIScreen.main.bounds.size.width - 20 - labe3.frame.maxX - 20, height: 20))
        slider3.addTarget(self, action: #selector(slider3Cnange), for: .valueChanged)
        slider3.minimumValue = -1
        slider3.maximumValue = 1
        slider3.value = 0
        slider3.minimumTrackTintColor = .green
        slider3.maximumTrackTintColor = .brown
        slider3.thumbTintColor = .yellow
        addSubview(slider3)
        
        slider4 = UISlider(frame: CGRect(x: labe4.frame.maxX + 20, y: labe4.frame.midY - 10, width: UIScreen.main.bounds.size.width - 20 - labe4.frame.maxX - 20, height: 20))
        slider4.addTarget(self, action: #selector(slider4Cnange), for: .valueChanged)
        slider4.minimumValue = 0
        slider4.maximumValue = 1
        slider4.value = 0.5
        slider4.minimumTrackTintColor = .green
        slider4.maximumTrackTintColor = .brown
        slider4.thumbTintColor = .yellow
        addSubview(slider4)
    }
    
    @objc func change(swi: UISwitch) {
        if beautyCameraBlock != nil {
            beautyCameraBlock!(swi.isOn)
        }
        if swi.isOn {
            print("打开")
            beautyLabel.text = "关闭美颜"
            beautyLabel.textColor = .green
        } else {
            print("关闭")
            beautyLabel.text = "打开美颜"
            beautyLabel.textColor = .red
        }
    }

    /// 磨皮
    @objc func slider1Cnange(slider: UISlider) {
        if beautySetingBlock != nil {
            beautySetingBlock!(1, slider.value)
        }
    }
    /// 磨皮
    @objc func slider2Cnange(slider: UISlider) {
        if beautySetingBlock != nil {
            beautySetingBlock!(2, slider.value)
        }
    }
    /// 磨皮
    @objc func slider3Cnange(slider: UISlider) {
        if beautySetingBlock != nil {
            beautySetingBlock!(3, slider.value)
        }
    }
    /// 磨皮
    @objc func slider4Cnange(slider: UISlider) {
        if beautySetingBlock != nil {
            beautySetingBlock!(4, slider.value)
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
